package com.github.base.controller;

import com.github.base.annotation.SysLog;
import com.github.base.dto.role.RoleDTO;
import com.github.base.service.RoleService;
import com.github.base.utils.Result;
import com.github.base.validated.Add;
import com.github.base.validated.Update;
import com.github.base.vo.role.RolePageListVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * 角色基础操作
 * @author GongShaopeng
 * create by 2021-01-28
 */

@RestController
@Tag(name = "角色模块")
@RequestMapping("/role")
@ApiSupport(author = "GongShaopeng")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleController {
    private final RoleService roleService;

    @PostMapping("/save")
    @Operation(summary = "新增角色")
    @ApiOperationSupport(order = 1)
    @SysLog(module = "角色模块", value = "新增角色")
    public Result<?> save(@Validated(Add.class) @RequestBody RoleDTO roleDTO) {
        roleService.save(roleDTO);
        return Result.ok();
    }

    @GetMapping("/pageList")
    @Operation(summary = "分页查询角色")
    @ApiOperationSupport(order = 2)
    public Result<?> pageList(@RequestParam(value = "current") @Parameter(description = "当前页码") @Min(1) Integer current,
                           @RequestParam(value = "size") @Parameter(description = "每页数据量") @Min(1) Integer size,
                           @RequestParam(value = "roleName", required = false) @Parameter(description = "角色名") String roleName,
                           @RequestParam(value = "usable", required = false) @Parameter(description = "可用状态")
                               @Min(0) @Max(1) Integer usable,
                           @RequestParam(value = "roleDesc", required = false) @Parameter(description = "角色描述") String roleDesc) {
        if (roleName != null) roleName = roleName.trim();
        if (roleDesc != null) roleDesc = roleDesc.trim();
        Page<RolePageListVO> page = roleService.pageList(current, size, roleName, usable, roleDesc);
        return Result.ok(page);
    }

    @PostMapping("/updateById")
    @Operation(summary = "修改角色")
    @ApiOperationSupport(order = 3)
    @SysLog(module = "角色模块", value = "修改角色")
    public Result<?> updateById(@Validated(Update.class) @RequestBody RoleDTO roleDTO) {
        roleService.update(roleDTO);
        return Result.ok();
    }

    @PostMapping("/deleteById/{roleId}")
    @Operation(summary = "删除角色")
    @ApiOperationSupport(order = 4)
    @SysLog(module = "角色模块", value = "删除角色")
    public Result<?> delete(@PathVariable("roleId") @NotNull Long roleId) {
        roleService.delete(roleId);
        return Result.ok();
    }

    @PostMapping("/toggle/usable")
    @Operation(summary = "切换角色可用状态")
    @ApiOperationSupport(order = 5)
    @SysLog(module = "角色模块", value = "切换角色可用状态")
    public Result<?> toggleUsable(@RequestParam(value = "roleId") @Parameter(description = "角色id") @NotNull Long roleId) {
        roleService.toggle(roleId);
        return Result.ok();
    }

    @PostMapping("/allot/permission/{roleId}")
    @Operation(summary = "角色资源分配")
    @ApiOperationSupport(order = 6)
    @SysLog(module = "角色模块", value = "角色资源分配")
    public Result<?> allotPermission(@RequestBody @Parameter(description = "资源id数组") @NotNull Integer[] permissionIds,
                                      @PathVariable(value = "roleId") @Parameter(description = "角色id") @NotNull Long roleId) {
        if (permissionIds.length < 1) return Result.ok();
        roleService.allotPermission(roleId, permissionIds);
        return Result.ok();
    }

    @GetMapping("/permissions")
    @Operation(summary = "查询资源id列表")
    @ApiOperationSupport(order = 7)
    public Result<Set<Long>> permissions(@RequestParam(value = "roleId") @Parameter(description = "角色id") @Min(1) Long roleId) {
        return Result.ok(roleService.permissions(roleId));
    }
}
